//
//  ShopController.m
//  HaoHaiSpell
//
//  Created by 郭健 on 16/6/13.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "ShopController.h"
#import "ShopCell.h"
<<<<<<< HEAD
#import "HeaderReusableView.h"
#import "DetailsController.h"
#import "IndexModel.h"

=======
#import "DetailsController.h"
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d


@interface ShopController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,retain) NSMutableDictionary *parameter; //网络请求参数
@property (nonatomic ,strong) NSMutableArray *dataArray;    //用于储存数据
@property (nonatomic,strong)NSMutableArray *colletionviewDataSource;
@property (nonatomic, strong) UICollectionView *collectionView;
<<<<<<< HEAD
=======
@property (nonatomic, assign) int start;                        //设置加载条数
@property (nonatomic,strong)NSMutableArray *titleArr;



>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d

@end

@implementation ShopController

<<<<<<< HEAD
=======
-(NSMutableDictionary *)parameter{

    if (!_parameter) {
        _parameter =[NSMutableDictionary dictionaryWithDictionary:@{@"site_id":@"311"}];
    }
    return _parameter;
}

>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCollectionView];
    
<<<<<<< HEAD
    [self fetchNetworkingWithURL:@"http://wfx.hostadmin.com.cn/app/app.php?op=mall&page_size=10&verify=e10adc3949ba59abbe56e057f20f883e" parameter:nil type:10];
=======
    
    NSString *str = @"http://wfx.hostadmin.com.cn/app/app.php?op=mall&page_size=10&verify=e10adc3949ba59abbe56e057f20f883e";
//    [self.parameter setValue:@"289" forKey:@"site_id"];
    
    [self fetchNetworkingWithURL:str parameter:self.parameter ];      //商城主页数据
    [self creatMJRefreshAndLoadUrl:MallIndexUrl];       //下拉刷新数据
    
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
}


-(void)setCollectionView{

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
<<<<<<< HEAD
    flowLayout.itemSize = CGSizeMake((kWidth-30)/2, (kHeight-30)/2.5);  //item大小
    flowLayout.minimumInteritemSpacing = 5;                        //最小列间距
    flowLayout.minimumLineSpacing = 10;                              //最小行间距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);     //分区间隔
=======
    flowLayout.itemSize = CGSizeMake((kWidth-15)/2, (kHeight-30)/2.5);  //item大小
    flowLayout.minimumInteritemSpacing = 5;                        //最小列间距
    flowLayout.minimumLineSpacing = 5;                              //最小行间距
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);     //分区间隔
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;   //滑动方向
    
    _collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];    //初始化collectionView
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
<<<<<<< HEAD
    
=======

>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    //注册cell
    [_collectionView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];

    _collectionView.backgroundColor = [UIColor grayColor];
<<<<<<< HEAD
    
    //注册头部视图
    [_collectionView registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    
    //注册尾部视图
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    
=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    [self.view addSubview:_collectionView];

}



#pragma mark -- 懒加载
<<<<<<< HEAD
-(NSMutableDictionary *)parameter{

    if (!_parameter) {
        _parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"":@1,@"":@3}];
    }
    return _parameter;
}

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
-(NSMutableArray *)dataArray{

    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

<<<<<<< HEAD
- (NSMutableArray*)colletionviewDataSource{
    if (!_colletionviewDataSource) {
        _colletionviewDataSource = [NSMutableArray array];
    }
    return _colletionviewDataSource;
}


#pragma mark -- 网络请求
- (void)fetchNetworkingWithURL:(NSString *)url parameter:(NSMutableDictionary*)parameter type:(NSInteger)type{
=======





#pragma mark -- 网络请求
- (void)fetchNetworkingWithURL:(NSString *)url parameter:(NSMutableDictionary*)parameter{
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
       
        NSMutableArray *array = dic[@"content"];
        
        for (NSDictionary *dic_1 in array) {
            
            IndexModel *index = [[IndexModel alloc]init];
            
            [index setValuesForKeysWithDictionary:dic_1];
            
            [self.dataArray addObject:index];
            
        }
<<<<<<< HEAD
        NSLog(@"++++++++%@",self.dataArray);
        [_collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        
        NSLog(@"数据请求失败");
    }];
    

=======
//        NSLog(@"++++++++%@",self.dataArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_collectionView reloadData];
                [self.collectionView.mj_footer endRefreshing];
                [self.collectionView.mj_header endRefreshing];
            });

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        NSLog(@"数据请求失败");
    }];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
}




<<<<<<< HEAD
#pragma mark -- UICollectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

=======




#pragma mark -- 下拉加载
-(void)creatMJRefreshAndLoadUrl:(NSString *)urlStr{

    //上拉加载
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //    重新设置加载条数
        self.start  += 10;
        [self.parameter setObject:@(self.start) forKey:@"start"];
        [self fetchNetworkingWithURL:urlStr parameter:self.parameter];
        
    }];
    
    //下拉刷新
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        //每次刷新之前将之前的数据移除
        if (self.dataArray.count != 0) {
            self.dataArray = nil;
        }
        [self fetchNetworkingWithURL:urlStr parameter:self.parameter];
        [self.collectionView reloadData];
        
    }];
}



#pragma mark -- UICollectionView的代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    
    
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    return self.dataArray.count;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   
    
    ShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    
    IndexModel *indexM = self.dataArray[indexPath.row];

    [cell setModel:indexM];
<<<<<<< HEAD
    
    cell.backgroundColor = [UIColor whiteColor];
=======


    
    cell.backgroundColor = [UIColor whiteColor];
    
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"第%ld个item",(long)indexPath.row);
    
    DetailsController *detailsVC = [[DetailsController alloc]init];
    detailsVC.title = @"商品详情";
<<<<<<< HEAD
    
    detailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    //判断返回是头部视图还是尾部视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        //初始化头部视图
        HeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        
        //设置头部视图样式
        headerView.backgroundColor = [UIColor whiteColor];
        
        return headerView;
    }else{
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
        
        //返回尾部视图
        footerView.backgroundColor = [UIColor whiteColor];
        
        return footerView;
    }

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    
    
    return CGSizeMake(150, 50);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

    
    return CGSizeMake(100, 50);
}




=======
    detailsVC.model = self.dataArray[indexPath.row];
    

    
 
    
    detailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}


>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
